<?php
	class ViewsHelper extends EngageHelpers {
		private $view_name = "";
		private $view_title = "";
		private $vars = "";
		var $actions = "";

		
		function load($name, $_title = NULL, $_vars = NULL, $theme = NULL){
			if($theme == ""){
				$theme = $this->site['theme'];
			}else{
				$this->site['theme'] = $theme;
			}
						
			$_shell = $this->site['root']."site/themes/".$theme."/shell.php";
			$theme = "";
			$file_exists = file_exists($_shell);
			if($file_exists){
				$this->view_name = $name;
				$this->view_title = $_title;
				$this->vars = $_vars;
				$_title = NULL;
				$name = NULL;
				$_vars = NULL;
				ob_start();
				include($_shell);
				$return = ob_get_clean();
				return $return;
			}
		}
		
		function get_title($return = false){
			if($return){
				return $this->view_title;
			}else{
				echo $this->view_title;
			}
		}
		
		function get_css($return = false){
			$css_url = $this->site['url']."site/themes/".$this->site['theme']."/css/style.css";
			if($return){
				return $this->tag("link/", array("rel" => "stylesheet", "type" => "text/css", "href" => $css_url), "", true);
			}else{
				$this->tag("link/", array("rel" => "stylesheet", "type" => "text/css", "href" => $css_url));
			}
		}
		
		function get_vars(){
			return $this->vars;
		}
		
		function tag($tag, $attributes = array(), $content = false, $return = false){
			$cs = "";
			$tag = str_replace(" ", "", $tag);
			if(strpos($tag, "/") !== false){
				$tag = str_replace("/", "", $tag);
				$cs = " /";
			}
				
			$ret = "<";
			$ret .= $tag;
			
			if(is_array($attributes)){
				foreach($attributes as $attr => $val){
					$ret.= " ".$attr."=\"".$val."\"";
				}
			}
			
			$ret .= $cs.">";
			
			if($content != false){
				$ret .= $content;
				$ret .= "</".$tag.">";
			}
			
			$ret .= "\r\n";
			
			if($return){
				return $ret;
			}else{
				echo $ret;
			}
			
		}
		
		function load_view(){
			$_view = $this->site['module']['views']['root']."html/".$this->view_name;
			$file_exists = file_exists($_view);
			
			if($file_exists){
				include($_view);
			}
		}
		
	}
?>